;(function ($) {
    var basePath = $('#apiUrl').val(),
        rem = 50;
    $.eventAlert = function (messageObj){
        /* 添加模态框 */
        var model = $("#event-alert");
        if (!model.length) {
            /* iziModal模态窗口:报修处理 */
            $('body').append('<div id="event-alert" class="score" style="background:#f5f5f5;">' +
                '<div class="score-797987978">' +
                    '<div class="box-body" style="overflow-y:auto;overflow-x: hidden;padding-top:0.3rem;">' +
                        '<div class="col-md-12">' +
                            '<div class="col-md-6" style="height: 100%;">' +
                                '<div class="col-md-12"><b class="text-aqua big-title">基本信息</b></div>' +
                                '<div class="col-md-6 thumb thumbnail1" style="display: none;">' +
                                    '<div class="prev"><img src="' + basePath + '/images/prev.png"><span></span></div>' +
                                    '<ul><li style="height: 3rem;"><img src="' + basePath + '/images/next.png"></li></ul>' +
                                    '<div class="next"><img src="' + basePath + '/images/next.png"><span></span></div>' +
                                '</div>' +
                                '<div class="col-md-6">' +
                                    '<p></p>' +
                                    '<p><a class="bxclAssetsName big-title spanTitle">&nbsp;</a></p>' +
                                    '<p><span class="text-muted">设备型号：</span><span class="bxclModel"></span></p>' +
                                    '<p><span class="text-muted">医院：</span><span class="bxclHospital"></span></p>' +
                                    '<p><span class="text-muted">科室：</span><span class="bxclDepartment"></span></p>' +
                                '</div>' +
                            '</div>' +
                            '<div class="col-md-6" style="height:100%;">' +
                                '<div class="col-md-12"><b class="text-aqua big-title">报修信息</b></div>' +
                                '<div class="col-md-6 thumb thumbnail2" style="display: none;">' +
                                    '<div class="prev"><img src="' + basePath + '/images/prev.png"><span></span></div>' +
                                    '<ul><li style="height: 3rem;"><img src="' + basePath + '/images/next.png"></li></ul>' +
                                    '<div class="next"><img src="' + basePath + '/images/next.png"><span></span></div>' +
                                '</div>' +
                                '<div class="col-md-6">' +
                                    '<p></p>' +
                                    '<p><b  class="big-title">设备故障</b></p>' +
                                    '<p><span class="text-muted">故障时间：</span><span class="bxclFaultTime"></span></p>' +
                                    '<p><span class="text-muted">故障描述：</span><span class="bxclFault"></span></p>' +
                                    '<p><span class="text-muted">报修人：</span><span class="bxclRepairPlatform"></span></p>' +
                                '</div>' +
                            '</div>' +
                        '</div>' +
                        '<div class="col-md-12 repair-history">' +
                            '<div class="col-md-12"><b class="text-aqua col-md-12 big-title">近期维修</b></div>' +
                            '<p></p>' +
                            '<div class="col-md-12">' +
                                '<table class="table repair-history-table"><thead></thead><tbody></tbody></table>' +
                            '</div>' +
                        '</div>' +
                    '</div>' +
                    '<div class="box-footer text-center" style="display: flex;justify-content: space-between;padding: 0 2.0rem;line-height: 0.6rem;">' +
                        '<div>' +
                            '<img class="deal prev-deal" src="' + basePath + '/images/prev.png" alt="上一个">' +
                            '<span class="progress page text-muted" style="height: auto;margin: 0.4rem 0;color:#303030;"></span>' +
                            '<img class="deal next-deal" src="' + basePath + '/images/next.png" alt="下一个">' +
                        '</div>' +
                        '<button data-iziModal-close class="btn btn-default cancel">稍后处理</button>' +
                        '<button type="button" class="btn save">立即处理</button>' +
                    '</div>' +
                '</div>' +
            '</div>');
            model = $("#event-alert");
            model.iziModal({
                title: '报修事件提醒',
                headerColor: '#fff',
                theme: '',
                attached: '',
                icon: null,
                iconText: null,
                iconColor: '#4c4c4c',
                rtl: false,
                width: 20 * rem,
                padding: 0,
                radius: 0.4 * rem,
                zindex: 1030,
                iframe: false,
                iframeHeight: 8 * rem,
                iframeURL: null,
                focusInput: false,
                group: '',
                loop: false,
                navigateCaption: true,
                navigateArrows: true,
                history: true,
                restoreDefaultContent: false,
                autoOpen: false,
                bodyOverflow: true,
                fullscreen: false,
                openFullscreen: false,
                closeOnEscape: false,
                overlay: true,
                overlayClose: false,
                overlayColor: 'rgba(0, 0, 0, 0.4)',
                timeout: false,
                timeoutProgressbar: false,
                pauseOnHover: false,
                timeoutProgressbarColor: 'rgba(255,255,255,0.5)',
                transitionIn: 'comingIn',
                transitionOut: 'comingOut',
                transitionInOverlay: 'fadeIn',
                transitionOutOverlay: 'fadeOut',
                onFullscreen: function(){},
                onResize: function(){},
                onOpening: function(){},
                onOpened: function(){},
                onClosing: function(){},
                onClosed: function(){
                    model.remove();
                }
            });
        }

        model.iziModal('open');

        if (messageObj.deviceId) queryDeviceInfo(messageObj.deviceId);
        queryRepairInfo(messageObj.repairId);

        /* 提交 */
        if (messageObj.isDoNow) {
            model.find('.save').attr('disabled', 'disabled').off();
        } else {
            model.find('.save').off().click(function () {
                model.iziModal('close');
                $.repairPop(messageObj.deviceId, messageObj.repairId);
            });
        }

        model.find('.cancel, .iziModal-button-close').off().click(function (e){
            e.stopPropagation();
            messageObjArr[messageObjArr.index].isDoNow = true;
            messageObjArr.index++;
            if (messageObjArr.index < messageObjArr.length) {
                $.eventAlert(messageObjArr[messageObjArr.index]);
            } else {
                messageObjArr.index = messageObjArr.length - 1;
                model.iziModal('close');
            }
        });
        /* 进度显示 */
        model.find('.progress').html(messageObjArr.index + 1 + '/' + messageObjArr.length + '条');
        /* 上一个下一个 */
        var progressEl = model.find('.progress.page');
        model.find('.deal').off().click(function () {
            var obj = $(this);
            var index = progressEl.text().split('/')[0] - 1;
            if (obj.hasClass('prev-deal')) {
                index--;
                index = index < 0 ? messageObjArr.length - 1 : index;
                messageObjArr.index = index;
                $.eventAlert(messageObjArr[index]);
            } else if (obj.hasClass('next-deal')) {
                index++;
                index = index % messageObjArr.length;
                messageObjArr.index = index;
                $.eventAlert(messageObjArr[index]);
            }
        });

        // 查询设备详情
        function queryDeviceInfo(deviceId) {
            publicAjax(basePath + 'device/queryDeviceInfo', 'post', {deviceId: deviceId},function (data) {
                console.log(data);
                if (data.ret === 200) {
                    model.find('.bxclAssetsName').text(data.data.deviceName || '')
                        .attr('href', basePath + 'device/info?id=' + data.data.deviceId);
                    model.find('.bxclModel').text(data.data.deviceModel || '');
                    model.find('.bxclHospital').text(data.data.hospitalName || '');
                    model.find('.bxclDepartment').text(data.data.department || '');
                    if (data.data.fileList) {
                        assetsPhotos = '';
                        for (var i = 0; i < data.data.fileList.length; i++) {
                            if (data.data.fileList[i].fileTypeName === '设备图片') {
                                assetsPhotos += data.data.fileList[i].fileUrl + ',';
                            }
                        }
                        thumb(model.find('.thumbnail1'), assetsPhotos);
                    }
                    //绑定维修历史信息
                    var strs = "";
                    var obj = data.data;
                    if(obj.repairRecords == null || obj.repairRecords.length == 0){
                        strs = "<tr><td colspan='5' class='text-muted text-center'>暂无维修记录</td></tr>";
                    }else{
                        $.each(obj.repairRecords,function (index,item) {
                            var fileName = item.fileName + "." + item.fileSuffix;
                            strs += "<tr><td class='text-muted text-center'>"+(index+1)+"</td>" +
                                "<td class='text-muted text-center'><button type='button' class='btn btn-primary btn-xs' onclick='queryRepairInfo("+item.repairId+")' data-original-title='详情'><i class='fa fa-file-text-o'></i></button>" +
                                "<td class='text-muted text-center'>"+(item.faultTime == null?'':item.faultTime)+"</td>" +
                                "<td class='text-muted text-center'>"+(item.repairTime == null?'':item.repairTime)+"</td>" +
                                "<td class='text-muted text-center'>"+(item.statusStr == null?'':item.statusStr)+"</td>" +
                                "</tr>";
                        });
                    }
                    model.find(".repair-history-table tbody").html(strs);
                } else {
                    flavrShowByTime(data.msg,null,"danger",false);
                }
            });
        }

        /* 查询维修详情 */
        function queryRepairInfo(repairId) {
            publicAjax(basePath + 'repair/details', 'post', {repairId: repairId},function (data) {
                console.log(data);
                if (data.ret === 200) {
                    model.find('.bxclFaultTime').text(data.data.repairTime || '');
                    model.find('.bxclFault').text(data.data.faultDepictHospital || '');
                    model.find('.bxclRepairPlatform').text(data.data.repairPerson || '');
                    if (data.data.fileList) {
                        assetsPhotos = '';
                        for (var i = 0; i < data.data.fileList.length; i++) {
                            if (data.data.fileList[i].fileTypeName === '故障图片') {
                                assetsPhotos += data.data.fileList[i].fileUrl + ',';
                            }
                        }
                        thumb(model.find('.thumbnail2'), assetsPhotos);
                    }
                } else {
                    flavrShowByTime(data.msg,null,"danger",false);
                }
            });
        }

        /* 缩略图 */
        function thumb(thumbnail2, assetsPhotos) {
            var assetsPhotos = assetsPhotos.split(',');
            if (!assetsPhotos[assetsPhotos.length - 1]) {
                assetsPhotos.pop();
            }
            if (assetsPhotos.length > 0) {
                thumbnail2.find('ul img').attr('src', assetsPhotos[0]).viewer({zIndex: 10011});
                var prev = thumbnail2.find('.prev').css('visibility', 'hidden');
                var next = thumbnail2.find('.next').css('visibility', 'hidden');
                var index = 0;
                if (assetsPhotos.length > 1) {
                    next.css('visibility', 'visible');
                }
                next.click(function () {
                    index++;
                    index %= assetsPhotos.length;
                    if (index === assetsPhotos.length - 1) {
                        next.css('visibility', 'hidden');
                        prev.css('visibility', 'visible');
                    } else if (index > 0) {
                        prev.css('visibility', 'visible');
                    }
                    thumbnail2.find('ul img').attr('src', assetsPhotos[index]);
                });
                prev.click(function () {
                    index--;
                    index = index < 0 ? assetsPhotos.length - 1 : index;
                    if (index === 0) {
                        prev.css('visibility', 'hidden');
                        next.css('visibility', 'visible');
                    } else if (index < assetsPhotos.length - 1) {
                        next.css('visibility', 'hidden');
                    }
                    thumbnail2.find('ul img').attr('src', assetsPhotos[index]);
                });
                thumbnail2.show();
            } else {
                thumbnail2.hide();
            }
        }
    };
})(jQuery);